package com.ahdms.es.engine;

import com.ahdms.es.bean.SesSignInfo;
import com.ahdms.es.exceptions.OFDVerifyException;
import com.ahdms.es.result.VerifyResult;
import com.ahdms.es.util.NioUtils;
import com.ahdms.es.util.OfdVerifyUtils;
import org.ofdrw.reader.OFDReader;

import java.io.Closeable;
import java.io.IOException;
import java.util.List;

/**
 * @author qinxiang
 * @date 2021-04-14 15:00
 */
public class OfdFileVerifyEngine extends FileVerifyEngine implements Closeable {

    private final OFDReader ofdReader;

    public OfdFileVerifyEngine(byte[] fileData) throws IOException {
        ofdReader = new OFDReader(NioUtils.toInputStream(fileData));
    }

    @Override
    public List<VerifyResult<SesSignInfo>> verify() throws OFDVerifyException {
        try {
            return OfdVerifyUtils.verify(ofdReader);
        } finally {
            close();
        }
    }

    @Override
    public void close() {
        try {
            ofdReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
